<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      size="medium"
      :rules="rules"
      :model="loginForm"
      class="loginForm"
    >
      <el-form-item label>
        <div class="login-title">22软工1班第8组物业管理系统</div>
      </el-form-item>
      <el-form-item prop="username" label>
        <el-input
          v-model="loginForm.username"
          type="text"
          placeholder="请输入用户名"
        />
      </el-form-item>
      <el-form-item prop="password" label>
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item prop="userType">
        <el-radio-group v-model="loginForm.userType">
          <el-radio :label="0" @change="changeRole('yezu')">业主</el-radio>
          <el-radio :label="1" @change="changeRole('wuye')">物业</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-button
              class="my-button"
              type="primary"
              @click="onSubmit()"
            >登录</el-button>
          </el-col>
          <el-col :span="12">
            <el-button class="my-button">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imgSrc: '',
      loginForm: {
        username: 'admin', // 登录名
        password: '123456', // 密码
        userType: 1 // 用户类型 0：业主 1：物业
      },
      rules: {
        username: [
          {
            required: true,
            trigger: 'change',
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'change',
            message: '请输入密码'
          }
        ],
        userType: [
          {
            required: true,
            trigger: 'change',
            message: '请选择用户类型'
          }
        ]
      }
    }
  },
  created() {},
  methods: {
    // 登录按钮
    async onSubmit() {
      // 表单验证
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          // 调用store里面的login方法
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              // 登录成功，跳转路由
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    changeRole(type) {
      if (type === 'yezu') {
        this.loginForm = {
          username: 'zxy', // 登录名
          password: '123456', // 密码
          userType: 0 // 用户类型 0：业主 1：物业
        }
      } else if (type === 'wuye') {
        this.loginForm = {
          username: 'admin', // 登录名
          password: '123456', // 密码
          userType: 1 // 用户类型 0：业主 1：物业
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  background-image: url("../../assets/images/login_bg.png");
}

.loginForm {
  margin: auto;
  height: 350px;
  width: 450px;
  border-radius: 10px;
  box-shadow: 0 0 25px #cac6c6;
  padding: 20px 35px;
  background-color: #fff;
}
.login-title {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
}
.my-button {
  width: 100%;
}
</style>
